<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="yinhang" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                    <el-dropdown-item>编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形布局 -->
        <el-tree
          :data="data"
          :default-expand-all="true"
          :props="defaultProps"
        >
          <span slot-scope="{ data,node }" style="width:100%">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
              <el-col :span="20">
                <svg-icon v-if="node.expanded" :icon-class="data.children.length?'del':'ren'" />
                <svg-icon v-else :icon-class="data.children.length?'addbox':'ren'" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

          </span>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加弹窗 -->
    <el-dialog
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <AddorEdit
        v-if="showDialog"
        :id="curId"
        ref="deptDialog"
        :on-list="onList"
        :is-edit="isEdit"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import AddorEdit from './deptDialog.vue'
import { toTree } from '@/utils/index'
import { getDepartments, delDepartment } from '@/api/departments'
export default {
  components: { AddorEdit },
  data() {
    return {
      onList: [],
      data: [],
      curId: '',
      showDialog: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      isEdit: ''
    }
  },
  created() {
    this.getDepartmentsApi()
  },
  methods: {
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.getDepartmentsApi()
    },
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.curId = id
    },
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.curId = id
      // this.$nextTick(() => {
      //   // 调用子组件的方法
      //   this.$refs.deptDialog.getDepartDetailApi()
      // })
    },
    async hDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.getDepartmentsApi()
      } catch (err) {
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    // 获取树形结构
    async getDepartmentsApi() {
      try {
        const res = await getDepartments()
        this.data = toTree(res.data.depts)
        this.data.shift()
        this.onList = res.data.depts.map(item => {
          return { id: item.id, code: item.code, name: item.name, pid: item.pid }
        })
      } catch (err) {
        console.log(err)
      }
    },
    // 收到子组件中的close事件
    hClose() {
      this.showDialog = false
      // this.$nextTick(() => {
      //   this.$refs.deptDialog.resetForm()
      // })
    }
  }
}
</script>
<style>
  .el-icon-caret-right{
display: none;
  }
</style>
